package TestServlet;

import dao.Db;
import model.User;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/my.do")
public class MyServlet extends MyBaseServlet{
    //逻辑1
    public void delDo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        System.out.println("这是我删除的逻辑操作");
        //获取删除的那条记录的主键Id
        String id=request.getParameter("id");
        System.out.println("id="+id);
        //访问数据层
        Db db = new Db();
        int i=db.del(id);
        System.out.println("i="+i);
        if (i>0){
            request.getRequestDispatcher("showlist").forward(request,response);
        }else {
            System.out.println("删除失败");
        }
    }
//得到要更改的数据
    public class updateOne  {
        public void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
            //设置编码
            servletRequest.setCharacterEncoding("UTF-8");
            servletResponse.setContentType("text/html;charset=UTF-8");
            Db db = new Db();
            //收参
            String id = servletRequest.getParameter("id");
            User user = db.getOneData(id);
            //服务器渲染表单
            PrintWriter writer = servletResponse.getWriter();
            writer.write("<!DOCTYPE html>");
            writer.write("<html>");
            writer.write("<head><meta charset='utf-8'><title>用户信息更新</title></head>");
            writer.write("<body>");
            writer.write("<form action='/javaWebStudy2025_war_exploded/my.do?method=UpdateOk' method='post'>");
            writer.write("<input type='hidden' value='"+user.getId()+"' name='id'><br>");
            writer.write("<input type='text' value='"+user.getUsername()+"' name='username'><br>");
            writer.write("<input type='text' value='"+user.getPassword()+"' name='password'><br>");
            writer.write("<input type='text' value='"+user.getBirthday()+"' name='birthday'><br>");
            writer.write("<input type='submit' value='提交'><br>");
            writer.write("</form>");
            writer.write("</body>");
            writer.write("</html>");
        }
    }
//更改得到的数据
    public class UpdateOk  {
        public void service(HttpServletRequest req, HttpServletResponse res )throws ServletException, IOException{
            //设置编码
            req.setCharacterEncoding("UTF-8");
            res.setContentType("text/html;charset=UTF-8");
            //收参
            Integer id=Integer.valueOf(req.getParameter("id"));
            String username=req.getParameter("username");
            String password=req.getParameter("password");
            String birthday=req.getParameter("birthday");
            //访问数据层
            User user=new User(id,username,password,birthday);
            Db db = new Db();
            int i=db.updateOneData(user);
            if (i>0){
                //跳转
                req.getRequestDispatcher("showlist").forward(req,res);
            }
        }
    }

}
